using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using TaugeLibrary.Classes;

namespace TaugeLibrary.UserControls
{
    public class DateTextBox:ExtendedTextBox 
    {
        private TaugeDateTime m_value = new TaugeDateTime();

        public DateTextBox()
            : base()
        {
            MaxLength = 10;
            IsNull = true;            
        }

        public bool IsNull
        {
            set
            {
                m_value.IsNull = value ;
                Text = m_value.DateText;
            }
            get
            {
                m_value.DateText = Text;
                return m_value.IsNull;
            }
        }

        public DateTime  Value
        {
            set
            {
                m_value.Value = value;
                Text = m_value.DateText;
                
            }
            get
            {
                m_value.DateText = Text;
                return m_value.Value;
            }
        }

        public TaugeDateTime HrValue
        {
            //set { m_value = value; }
            get { return m_value; }    
        }

        protected override void OnLostFocus(EventArgs e)
        {
            m_value.DateText = Text;
            Text = m_value.DateText;
            base.OnLostFocus(e);
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            //if((e.KeyCode==Keys.
            if ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9)
                ||(e.KeyCode>= Keys.NumPad0 && e.KeyCode<=Keys.NumPad9))
            {
                if (SelectionStart == 2 || SelectionStart == 5)
                    SelectionStart++;
                SelectionLength = 1;
                base.OnKeyDown(e);
            }
            else if (e.KeyCode == Keys.Back)
            {
                if (SelectionStart > 0)
                {
                    SelectionStart--;
                    if (SelectionStart == 2 || SelectionStart == 5)
                        SelectionStart--;
                    SelectionLength = 1;
                    SelectedText = "0";
                    SelectionStart--;
                }
                else if(SelectionStart==0 && SelectionLength==10)
                {
                    Text= "00/00/0000";
                    SelectionStart = 0;
                }
                e.Handled=true;
                
            }
            else if (e.KeyCode == Keys.Enter || e.KeyCode==Keys.Left || e.KeyCode==Keys.Right ||e.KeyCode==Keys.Home || e.KeyCode == Keys.End)
            {
                base.OnKeyDown(e);
            }
            else
            {
                e.Handled = true;
            }
            
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (
                    (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
                    || e.KeyChar == '\b'
                )
            {                
                    e.Handled = true;            
            }
            base.OnKeyPress(e);
        }

        
        
            
    }
}
